import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;


public class Visitor {
	Set<Person> visited = new HashSet<Person>(); 
	Person source;
	Person target;
	
	Visitor() {	}
	
	public boolean isRelated(Person source, Person target) {
		this.source= source;
		this.target= target;
		visited.clear();
		visited.add(source);
		return isRelatedHelper(source);
	}
	
	private boolean isRelatedHelper(Person source) {
		if (source.isRelated(target)) {
			return true;
		}
		if (isRelated(source.getParents().toArray())) {
			return true;
		}
		if (isRelated(source.getChildren().toArray())) {
			return true;
		}
		if (source.hasSpouse() ) {
			return source.getSpouse().isRelated(target);
		}
		else {
			return false;				
		}

	}
	
	private boolean isRelated(Object[] relatives) {
		for (int i=0; i<relatives.length; i++) {
			Person relative=null;
			try {
				relative=(Person)relatives[i];
			} catch (Exception e) {
			}
			if (relative!=null) {
				if (relative.isRelated(target)) {
					return true;
				}
			}
		}
		return false;
	}

}
